using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Diagnostics;
using Win32;

using WeifenLuo.WinFormsUI.Docking;
using PragmaTouchUtils;


namespace eBook.WinApp
{ 
  public partial class frmMain : Form
  {
    private ClipboardHook _clpHook;
    //private FormFactory _formFactory;

    public DockPanel DockPane{ get; set; }

    private ClipboardEventHandler _onClipboardText;
    public event ClipboardEventHandler OnClipboardText
    {
      add { _onClipboardText += value; }
      remove { _onClipboardText -= value; }
    }

    public frmMain()
    {
      try
      {
        this.Cursor = Cursors.AppStarting;
        
        this.InitializeComponent();        
        this.InitializeDockPanel();
        
        ConfigContent.ApplicationName = "eBookLibrary";
        ConfigContent.Current.LoadConfiguration();

        //_formFactory = FormFactory.Instance;
        FormFactory.Instance.MdiContainer = this;
        FormFactory.Instance.ShowDockContent = this.ShowDockContent;
        _clpHook = new ClipboardHook();
      }
      finally
      {
        this.Cursor = Cursors.Default;
      }
    }

    private void _clpHook_OnClipboardText(object sender, ClipboardEventArgs e)
    {
      if (_onClipboardText != null)
        _onClipboardText(sender, e);
    }

    protected override void WndProc(ref Message m)
    {
      bool handled = false;
      if ( _clpHook != null)
        handled = _clpHook.HandleClipboardMessage(m);

      if (!handled)
        base.WndProc(ref m);
    }


    private void InitializeDockPanel()
    {
      this.DockPane = new DockPanel();
      this.DockPane.ActiveAutoHideContent = null;
      this.DockPane.Dock = DockStyle.Fill;
      this.DockPane.Font = new System.Drawing.Font("Tahoma", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.World, ((byte)(0)));
      this.DockPane.Location = new System.Drawing.Point(0, 49);
      this.DockPane.Name = "_dockPanel";
      this.DockPane.Size = new System.Drawing.Size(579, 338);
      this.DockPane.TabIndex = 0;

      this.Controls.Add(this.DockPane);
      this.DockPane.BringToFront();
      this.DockPane.ShowDocumentIcon = true;
      this.DockPane.DocumentStyle = DocumentStyle.DockingMdi;
      //_dockPanel.DocumentStyle = ConfigContent.GeneralOptions.HostDocumentStyle;
    }


    private void frmMain_Load(object sender, EventArgs e)
    {
      _clpHook.RegisterClipboardViewer(this.Handle);
      _clpHook.OnClipboardText += new ClipboardEventHandler(_clpHook_OnClipboardText);
    }

    private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
    {
      _clpHook.UnregisterClipboardViewer(this.Handle);
      _clpHook.OnClipboardText -= new ClipboardEventHandler(_clpHook_OnClipboardText);
    }



    private void ShowDockContent(DockContent formInstance)
    {
      Application.DoEvents();
      this.Cursor = Cursors.AppStarting;
      try
      {
        formInstance.MdiParent = this;
        formInstance.WindowState = FormWindowState.Normal;
        formInstance.Show(this.DockPane, DockState.Document);
        formInstance.BringToFront();        
      }
      finally
      {
        this.Cursor = Cursors.Default;
      }
    }



    public void CloseDocuments(DockPanel dp, IList<Form> skipForms, bool closeFloatingWindows)
    {
      if (dp == null)
        return;
      
      if (dp.DocumentStyle == DocumentStyle.SystemMdi)
      {
        foreach (Form form in MdiChildren)
        {
          if (skipForms != null && skipForms.Contains(form))
            continue;

          form.Close();
        }
      }
      else if (dp.DocumentStyle == DocumentStyle.DockingMdi)
      {
        IList<FloatWindow> formsToClose = new List<FloatWindow>(dp.FloatWindows);
        foreach (Form form in formsToClose)
        {
          if (skipForms != null && skipForms.Contains(form))
            continue;
          form.Close();
        }
      }

      IList<IDockContent> docs = null;
      if (closeFloatingWindows)
        docs = new List<IDockContent>(dp.Contents);
      else
        docs = new List<IDockContent>(dp.Documents);
      
      foreach (IDockContent content in docs)
      {
        Form form = content as Form;
        if ( skipForms != null && skipForms.Contains(form) )
            continue;

        form.Close();
      }
        
    }

    public void OnFinalSelection(object sender, ConfigEventArgs args)
    {

    }


    private void mnuBooks_Click(object sender, EventArgs e)
    {
      var formInstance = FormFactory.Instance.LaunchDockContent<frmBook>(); 
    }

    private void mnuAuthor_Click(object sender, EventArgs e)
    {
      var formInstance = FormFactory.Instance.LaunchDockContent<frmAuthor>(); 
    }

    private void mnuImport_Click(object sender, EventArgs e)
    {
      var formInstance = FormFactory.Instance.LaunchDockContent<frmImport>(); 
    }







    private void mnuOptions_Click(object sender, EventArgs e)
    {
      frmConfigurationDlg.ShowConfigurationDlg(ConfigContent.Current, this, this.OnFinalSelection);
    }

    private void mnuAbout_Click(object sender, EventArgs e)
    {
      //(new frmAbout()).ShowDialog();
    }

    private void mnuExit_Click(object sender, EventArgs e)
    {
      this.CloseDocuments(this.DockPane, null, true);
      this.Close();
    }




    private void mnuResizeToIPhone_Click(object sender, EventArgs e)
    {
      foreach (var frm in this.MdiChildren)
	    {
        frm.Size = new Size(328, 536);
        frm.WindowState = FormWindowState.Normal;    		 
	    }
    }

    private void mnu800_Click(object sender, EventArgs e)
    {
      this.Size = new Size(800, 600);
      this.WindowState = FormWindowState.Normal;   
    }

    private void mnu1024_Click(object sender, EventArgs e)
    {
      this.Size = new Size(1024, 768);
      this.WindowState = FormWindowState.Normal;   
    }

    private void mnuShowHtml_Click(object sender, EventArgs e)
    {      
      Form frm = this.ActiveMdiChild;
      if (frm != null)
      {
        MethodInfo mti = frm.GetType().GetMethod("ShowHTML");
        if ( mti != null )
          mti.Invoke(frm, null);
      }
    }

    private void menuViewStack_Click(object sender, EventArgs e)
    {
      //LaunchChildForm < frmPeeker >(FormWindowState.Normal);
    }

















  }
}